package cc.vicp.djx314.starfood.service.dishes.impls;

import java.util.List;

import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import cc.vicp.djx314.starfood.service.dishes.RecommendDishesService;
import cc.vicp.djx314.starfood.dao.dish.DishMapper;
import cc.vicp.djx314.starfood.domain.dish.Dish;

/**
 * RecommendDishesServiceInter实现类
 * @author 水山清风
 *
 */
@Service
public class RecommendDishesServiceImpl implements RecommendDishesService {
	//~属性=====================================================================

	@Autowired
	private DishMapper dishMapper;  //Dish数据库操作类


	//~方法=====================================================================
	@Override
	@Transactional(readOnly = true)
	public List<Dish> getPreferentialDishes() {
		int offset = 0;
		RowBounds rowBounds = new RowBounds(offset, PREFERENTIAL_NUM);
		List<Dish> preferentialDishes = dishMapper.getCacheIndexDishes(PREFERENTIAL_NAME, rowBounds);

		Assert.isTrue(preferentialDishes != null && preferentialDishes.size() == PREFERENTIAL_NUM,
				"首页优惠特卖的菜式数量应该是" + PREFERENTIAL_NUM);
		return preferentialDishes;
		//return new ArrayList<Dish>();
	}

	@Override
	@Transactional(readOnly = true)
	public List<Dish> getHotSellDishes() {
		int offset = 0;
		RowBounds rowBounds = new RowBounds(offset, HOT_SELL_NUM);
		List<Dish> hotSellDishes = dishMapper.getCacheIndexDishes(HOT_SELL_NAME, rowBounds);

		Assert.isTrue(hotSellDishes != null && hotSellDishes.size() == HOT_SELL_NUM,
				"首页本店热销的菜式数量应该是" + HOT_SELL_NUM);
		return hotSellDishes;
		//return new ArrayList<Dish>();
	}

	@Override
	@Transactional(readOnly = true)
	public List<Dish> getCollectionDishes() {
		int offset = 0;
		RowBounds rowBounds = new RowBounds(offset, COLLECTION_NUM);
		List<Dish> collectionDishes = dishMapper.getCacheIndexDishes(COLLECTION_NAME, rowBounds);

		Assert.isTrue(collectionDishes != null && collectionDishes.size() == COLLECTION_NUM,
				"首页收藏最多的菜式数量应该是" + COLLECTION_NUM);
		return collectionDishes;
	}

	//~getter和setter=====================================================================
	public void setDishMapper(DishMapper dishMapper) {  //设置dishMapper
		this.dishMapper = dishMapper;
	}
}
